package com.rc.evcharger.dto.terminal;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.terminal.Result;
import com.rc.evcharger.common.utils.DateHelper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by Yang on 2018/12/21
 */
public class FaultCode implements Result {

    @ByteLength(length = 1)
    Integer number;//告警数量

    @ByteLength(length = 1)
    String measuringPoint; // 测量点号

    @ByteLength(length = 6)
    Date addTime;     //添加时间

    @ByteLength(length = 1)
    List<String> code = new ArrayList<>(); //告警编码;

    public String getMeasuringPoint() {
        return measuringPoint;
    }

    public void setMeasuringPoint(String measuringPoint) {
        this.measuringPoint = measuringPoint;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Date getAddTime() {
        return addTime;
    }

    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }

    public List<String> getCode() {
        return code;
    }

    public void setCode(List<String> code) {
        this.code = code;
    }

    /**
     * 解析告警报文，目前告警数量只有一个；
     *
     * @param dataStr
     * @return
     */
    @Override
    public List<FaultCode> getResult(String dataStr) {
        List<FaultCode> result = new ArrayList<>();
        FaultCode fcr = new FaultCode();
        StringBuilder sb = new StringBuilder("20");
        String data = dataStr.trim();
        // 获取告警数量;
        fcr.number = Integer.valueOf(data.substring(0, 2));
        data = data.substring(3);
        // 获取测量点号;
        fcr.measuringPoint = data.substring(0, 2);
        data = data.substring(3);
        //年
        sb.append(data.substring(0, 2));
        data = data.substring(3);
        //月
        sb.append("-").append(data.substring(0, 2));
        data = data.substring(3);
        //日
        sb.append("-").append(data.substring(0, 2));
        data = data.substring(3);
        //时
        sb.append(" ").append(data.substring(0, 2));
        data = data.substring(3);
        //分
        sb.append(":").append(data.substring(0, 2));
        data = data.substring(3);
        //秒
        sb.append(":").append("00");
        //添加时间
        fcr.addTime = DateHelper.stringToDate(sb.toString(), DateHelper.DATE_COMMON_FORMAT);
        //告警编码
        while (data.length() >= 2) {
            fcr.code.add(data.substring(0, 2));
            if (data.length() == 2) {
                data = data.substring(2);
            } else {
                data = data.substring(3);
            }

        }
        result.add(fcr);
        return result;
    }

    public static void main(String[] args) {
//        String aa = "01 00 18 06 15 08 44 A2 10";//01 00 18 06 15 08 44 A2 10
//        FaultCodeResult fcr = new FaultCodeResult();
//        List<FaultCodeResult> list = fcr.getResult(aa);
//        Pile pile = new Pile();
//        pile.setId(146l);
//        for (FaultCodeResult faultCodeResult : list) {
//            System.out.println(faultCodeResult.getMeasuringPoint() + faultCodeResult.getCode() + " " + faultCodeResult.getNumber());
//            PileFaultRecord pileFaultRecord = new PileFaultRecord(faultCodeResult, pile);
//            System.out.println(pileFaultRecord.getFault() + " " + pileFaultRecord.getAddTime() + pileFaultRecord.getFaultTime());
//        }

    }
}
